﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Tools
{
    public class Log
    {
        //日志文件所在路径
        private static string logPath = AppDomain.CurrentDomain.BaseDirectory+"mtw_v2_c.log";
        private object obj;
        //日志级别
        private LogType logtype=LogType.debug;
        //是否打开控制台
        private bool isconsole = true;
        public Log(object obj)
        {
            this.obj = obj;
        }
        public void info(String msg)
        {
            if (logtype == LogType.error)
            {
                return;
            }
            else
            {
                WriteLog(msg);
            }
        }
        public void error(String msg)
        {
            Console.ForegroundColor = ConsoleColor.DarkRed;
            WriteLog(msg);
            Console.ForegroundColor = ConsoleColor.White;
        }
        public void debug(String msg)
        {
            if (logtype == LogType.info || logtype == LogType.error)
            {
                return;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                WriteLog(msg);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }

        /// <summary>
        /// 写日志
        /// <param name="logType">日志类型</param>
        /// <param name="msg">日志内容</param>
        /// </summary>
        private  void WriteLog(string msg)
        {

            StreamWriter sw=null;
            try
            {
                String allmsg = logtype + "#" + obj.GetType().FullName + "#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg;
                if (isconsole)
                {
                    Console.WriteLine(allmsg);
                }
                sw = new StreamWriter(logPath,true);
                sw.WriteLine(allmsg);
            }
            catch
            {
            }
            finally
            {
                sw.Close();
            }
        }
    }
    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogType
    {
        info,  //堆栈跟踪信息
        debug,//警告信息
        error,  //错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息
    }
}
